import { selSetting, selState, saveSetting, useSetting  } from '@/services/setting';
import { errNotification, successNotification } from '@/utils/notification';

export default {
    namespace: 'setting',
  
    state: {
        setSettingList: [],
        isFresh: true,
        globalState:''
    },
  
    effects: {
      *fetchSaveSetting(param, { call, put }) { //保存设置
        const response = yield call(saveSetting, param);
        if(response.success){
            successNotification("设置成功","")
        }else{
            errNotification("设置失败","")
        }
        const response2 = yield call(selSetting, param);
        if(response2.success){
            yield put({
                type: 'setSettings',
                payload: response2.data,
            });
        }
      },
      *fetchUseSetting(param, { call, put }) { //应用配置
        const response = yield call(useSetting, param);
        if(response.success){
            successNotification("应用成功","")
        }else{
            errNotification("应用失败","")
        }
      },
      *fetchSelSetting(param, { call, put }) { //查询配置
        const response = yield call(selSetting, param);
        if(response.success){
            yield put({
                type: 'setSettings',
                payload: response.data,
            });
        }
      },
      *fetchSelState(param, { call, put }) { //查询连接状态
        const response = yield call(selState, param);
        if(response.success){
            yield put({
                type: 'setState',
                payload: response.data,
            });
        }
      },
    },
  
    reducers: {
      setSettings(state, action) {
        return {
            ...state,
            setSettingList: action.payload,
            isFresh: false
        };
      },
      setFresh(state, action) {
        return {
            ...state,
            isFresh: action.payload
        };
      },
      setState(state, action) {
        return {
            ...state,
            globalState: action.payload.global,
        };
      },
    },
  };